﻿using LINGYUN.Abp.Aliyun;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Volo.Abp.BlobStoring;
using Volo.Abp.Modularity;

namespace LINGYUN.Abp.BlobStoring.Aliyun
{
    [DependsOn(
        typeof(AbpBlobStoringModule),
        typeof(AbpAliyunModule))]
    public class AbpBlobStoringAliyunModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            Configure<AbpBlobStoringOptions>(options =>
            {
                context.Services.ExecutePreConfiguredActions(options);
                options.Containers.ConfigureAll((containerName, containerConfiguration) =>
                {
                    containerConfiguration.UseAliyun(aliyun =>
                    {
                        aliyun.BucketName = configuration[AliyunBlobProviderConfigurationNames.BucketName] ?? "";
                        aliyun.CreateBucketIfNotExists = configuration.GetSection(AliyunBlobProviderConfigurationNames.CreateBucketIfNotExists).Get<bool>();
                        aliyun.CreateBucketReferer = configuration.GetSection(AliyunBlobProviderConfigurationNames.CreateBucketReferer).Get<List<string>>();
                        aliyun.Endpoint = configuration[AliyunBlobProviderConfigurationNames.Endpoint];
                    });
                });
            });
        }
    }
}
